/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: UploadNetRequestManager.java
 *   @Date: 3/21/19 2:59 PM
 *   @Author: chenjun
 *   @Description:
 *   @Copyright（C）: 2019 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.sdk.data_manager;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import io.bhex.baselib.network.BParamsBuilder;
import io.bhex.baselib.network.HttpUtils;
import io.bhex.baselib.network.listener.SimpleResponseListener;
import io.bhex.baselib.network.params.PostParams;
import io.bhex.baselib.network.params.UploadParam;
import io.bhex.baselib.network.response.BaseResponse;
import io.bhex.baselib.network.response.ResponseListener;
import io.bhex.sdk.UrlsConfig;
import io.bhex.sdk.trade.bean.OpenOrderResponse;

public class UploadNetRequestManager {
    private static UploadNetRequestManager mInstance;
    public static UploadNetRequestManager GetInstance() {
        if (mInstance == null) {

            mInstance = new UploadNetRequestManager();
        }

        return mInstance;
    }

    private List<UploadParam> requestList = new ArrayList<>();

    UploadNetRequestManager(){


    }

    public synchronized void SetUploadRequestParam(UploadParam param){
        try{
            if(param != null)
                requestList.add(param);

            if(requestList.size() >= 20) {
                UploadRequestParam(new SimpleResponseListener<BaseResponse>() {
                });
                requestList.clear();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void UploadRequestParam(ResponseListener observer){
        try {
            PostParams.Builder builder = new BParamsBuilder().post();
            builder.url(UrlsConfig.UPLOADREQUEST_URL);
            PostParams params = builder.build();
            String body =  JSON.toJSONString(requestList);
            HttpUtils.getInstance().request(params, OpenOrderResponse.class, observer, body);
        }
        catch (Exception e){

        }
    }
}
